package cz.muni.fi.pb138.gscholarinfo;

import cz.muni.fi.pb138.gscholarinfo.classes.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 
 */
@WebServlet(name = "MainServlet", urlPatterns = {"/MainServlet"})
public class MainServlet extends HttpServlet {
    
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {            
        /*PrintWriter out = response.getWriter();
        
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Wrong URL</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Invalid URL</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }*/
        
        request.setAttribute("view", 0);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer view = 0;
        try
        {
            if(request.getParameterMap().containsKey("authorName"))
            {
                if(request.getParameterMap().containsKey("authorID"))
                {
                    view = 1;
                    Author author = new Author();
                    author.setGoogleId((String)request.getParameter("authorID"));
                    author.setName((String)request.getParameter("authorName"));
                    author.checkHIndex();
                    String graph = AuthorGraph.createGraph(author.getAuthorPublications());
                    request.setAttribute("author", author);
                    request.setAttribute("graph", graph);
                }
                else
                {
                    List<Author> authors = GSUtils.searchAuthor((String)request.getParameter("authorName"));
                    switch(authors.size())
                    {
                        case 0:
                            view = 2;
                            break;
                        case 1:
                            view = 1;
                            Author author = authors.get(0);
                            author.checkHIndex();
                            String graph = AuthorGraph.createGraph(author.getAuthorPublications());
                            request.setAttribute("author", author);
                            request.setAttribute("graph", graph);
                            break;
                        default:
                            view = 3;
                            request.setAttribute("authors", authors);
                            break;
                    }
                }

                /*
                Publication var = new Publication();
                var.setGoogleId("11335411829926156313");
                PublicationCitations pubCit = GSUtils.searchPublicationCitations(var, -1);
                pubCit.toString();

                response.getWriter().println("Success!");
                */
                //response.getWriter().println(GSUtils.getAuthorPublications(GSUtils.searchAuthor(request.getParameter("query")).get(0)).getPublications().size());
                //AuthorGraph.createGraph(autPubs);
                //AuthorGraph.createGraph(GSUtils.searchAuthorPublications(GSUtils.searchAuthor(request.getParameter("query")).get(0)));

                //List<Author> authors = GSUtils.searchAuthor();
            }
        }
        catch(IOException ex)
        {
            view = 4;
        }
        catch(Exception ex)
        {
            ex.toString();
            ex.toString();
        }
        finally
        {
            request.setAttribute("view", view);
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Google H-Index calculator servlet";
    }
}
